package com.uxwine.cmm;

import com.uxwine.alarm.main.Alarm;

import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.text.GetChars;

public abstract class BaseService extends Service {
	
	public static final String ACTION_START_ALRAM = "com.uxwine.alarm.START_ALARM";
	
	public static final String ACTION_CANCEL_ALRAM = "com.uxwine.alarm.CANCEL_ALARM";
	
	public static final String ACTION_ALRAM_WAKEUP = "com.uxwine.alarm.ALARM_WAKEUP";
	
	public static final String ACTION_INIT_ALARM = "com.uxwine.alarm.INIT_ALARM";
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public int onStartCommand(Intent i, int flags, int startId)
	{
		if(null == i)
		{
			onServiceStart();
		}
		else
		{
			if(ACTION_INIT_ALARM.equals(i.getAction()))
			{
				onServiceStart();
			}
			else
			{
				onServiceCmd(i);
			}
		}		
		return super.onStartCommand(i, flags, startId);
	}
	
	abstract protected void onServiceCmd(Intent i);
	
	abstract protected void onServiceStart();
	
	public static void startAlarm(Context ctx, Alarm a)
	{
		Intent i = new Intent(ACTION_START_ALRAM);
		i.putExtra("alarm", a.getId());
		ctx.startService(i);
	}
	
	public static void cancelAlarm(Context ctx, Alarm a)
	{
		Intent i = new Intent(ACTION_START_ALRAM);
		i.putExtra("alarm", a.getId());
		ctx.startService(i);
	}
	
	public static void startService(Context ctx)
	{
		Intent i = new Intent(ACTION_INIT_ALARM);
		ctx.startService(i);
	}
}
